import {Router} from 'express';
import {makeModel, makeController} from '../needs'
import config from '../config/globalConfig';

const router = Router();
const controllers = require('../controllers');

/**
 * 创建路由对象数据
 * @returns {Router}
 */
export function createRouter() {
    Object.keys(controllers).forEach((keyName) => {
        let keyItem = controllers[keyName];
        if (config.dirNameList.indexOf(keyName) > -1) {
            Object.keys(keyItem).forEach(childKeyName => {
                router.use('/' + childKeyName, keyItem[childKeyName]);
            })
        } else {
            router.use('/' + keyName, keyItem);
        }
    });
    //创建model文件
    router.get('/public/makeModel', (req, res) => {
        res.json(makeModel())
    });

    //创建controller文件
    router.get('/public/makeController', (req, res) => {
        res.json(makeController())
    });

    router.get('/public/helloworld', (req, res) => {
        res.json({code: 1, msg: 'hello world'});
    })
    return router;
}
